package xyz.mkkbjb.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.sf.json.JSONObject;
import xyz.mkkbjb.config.AppConfig;
import xyz.mkkbjb.http.HttpRobot;
import xyz.mkkbjb.mapper.TakingInfoMapper;
import xyz.mkkbjb.pojo.TuLingAiAnswer;
import xyz.mkkbjb.pojo.UserInfo;
import xyz.mkkbjb.service.TuLingAiService;
@Service
@Transactional
public class TuLingAiServiceImpl implements TuLingAiService {
	@Autowired
	private TakingInfoMapper takingMapper;
	@Autowired
	private AppConfig appConfig;
	@Override
	public TuLingAiAnswer saveAiAnswer(String text, Integer reqType,UserInfo userIn) {
		if(text.contains("老板是")||text.contains("主人是")) {
			TuLingAiAnswer answer = new TuLingAiAnswer();
			answer.setAnswerCode(114);;
			answer.setAnswer("名字是莫");
			return answer;
		}
		if(text.contains("带你走")) {
			TuLingAiAnswer answer = new TuLingAiAnswer();
			answer.setAnswerCode(114);;
			answer.setAnswer("请发邮箱至mkkbjb@yeah.net,备注为：我要带小莫走");
			return answer;
		}
		if(text.contains("源码")) {
			TuLingAiAnswer answer = new TuLingAiAnswer();
			answer.setAnswerCode(114);;
			answer.setAnswer("请发邮箱至mkkbjb@yeah.net,备注为：源码");
			return answer;
		}
		userIn.setKey(appConfig.getRobotKey());
		JSONObject transJosn = HttpRobot.transJosn(text, reqType, userIn);
		TuLingAiAnswer answer = HttpRobot.postJson(appConfig.getRobotUrl(), transJosn.toString());
		if (answer == null ) {
			answer = new TuLingAiAnswer();
			answer.setAnswer("超出认知权限了");
			answer.setAnswerCode(200);
			return answer;
		}
		if (answer.getAnswer() == null) {
			answer.setAnswer("超出认知权限了");
			return answer;
		}
		answer.setPageId(userIn.getUserId());
		answer.setRecordTime(new Date());
		answer.setQuestion(text);
		takingMapper.save(answer);
		return answer;
	}

}
